/**
 * Copyright (c) 2016-2019 优博信息科技 All rights reserved.
 *
 * https://www.jnyoubo.com
 *
 * 版权所有，侵权必究！
 */

package com.youbo.common.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回数据
 *
 * @author macy
 */
public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;


	public static final int OK_SUCCESS=10001;
	public static final int ERROR2=10002;
	public static final int ERROR3=10003;
	public static final int ERROR9=10009;

	public R() {
		put("success", true);
		put("code",OK_SUCCESS);
		put("msg", "success");
		put("data", null);

	}
	
	public static R error() {
		return error(ERROR3, "未知异常，请联系管理员");
	}
	
	public static R error(String msg) {
		return error(ERROR3, msg);
	}
	
	public static R error(int code, String msg) {
		R r = new R();
		r.put("success", false);
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static R ok(String msg) {
		R r = new R();
		r.put("success", true);
		r.put("code", OK_SUCCESS);
		r.put("msg", msg);
		return r;
	}
	public static R ok(int code,String msg) {
		R r = new R();
		r.put("success", true);
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}


	public static R ok(Map<String, Object> map) {
		R r = new R();
		r.putAll(map);
		return r;
	}
	
	public static R ok() {
		return new R();
	}

	@Override
	public R put(String key, Object value) {
		super.put(key, value);
		return this;
	}

	public R putData(String key,Object value) {
		super.put(key, value);
		return this;
	}
	public R putData(Object value) {
		super.put("data", value);
		return this;
	}

	public Integer getCode() {
		return (null!=super.get("code"))?Integer.valueOf(super.get("code").toString()):null;
	}

	public Object getData() {
		return super.get("data");
	}

	public boolean getSuccess() {
		return (null!=super.get("success"))?Boolean.valueOf(super.get("success").toString()):false;
	}
}
